#include "connection.hpp"
#include "ansyncpool.hpp"
#include <functional>
using namespace ns_rabbitmq;
void task(const ChannelPtr &channel, const std::string consumer_tag, const BasicProperties *bp, const std::string &body)
{
    std::cout << consumer_tag << ":消费了消息:" << body << std::endl;
    // 响应
    channel->BasicAck(bp->id());
}

using Map = google::protobuf::Map<std::string, std::string>;

int main(int argc, char *argv[])
{
    // 1.创建线程池
    AnsyncPoolPtr pools = std::make_shared<AnsyncPool>();
    // 2.创建连接管理
    ConnectionPtr conn = std::make_shared<Connection>("127.0.0.1", 8085, pools);
    // 3.打开信道
    ChannelPtr channel = conn->OpenChannel();
    // 4.创建信道的服务
    //   1.交换机 2.队列 3.绑定 4.推送
    channel->DeclareExchange("exchange1", ExchangeType::FANOUT, true, false, Map());
    channel->DeclareExchange("exchange2", ExchangeType::DIRECT, true, false, Map());
    channel->DeclareExchange("exchange3", ExchangeType::TOPIC, true, false, Map());
    channel->DeclareQueue("queue1", false, true, false, Map());
    channel->DeclareQueue("queue2", false, true, false, Map());
    channel->Bind("exchange1", "queue1", "queue1", true);
    channel->Bind("exchange1", "queue2", "news.music.#", true);

    channel->Bind("exchange2", "queue1", "queue1", true);
    channel->Bind("exchange2", "queue2", "news.music.#", true);

    channel->Bind("exchange3", "queue1", "news.music.#", true);
    channel->Bind("exchange3", "queue2", "news.#", true);
    // 5.封装任务类型
    auto func = std::bind(task, channel, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3);
    channel->BasicConsume("consumer1", argv[1], false, func);

    // 阻塞等待
    while (1)
        std::this_thread::sleep_for(std::chrono::seconds(3));
    //  6.关闭信道
    conn->CloseChannel(channel);
    return 0;
}